package com.campus.server.service;


import com.campus.common.vo.Book;
import com.campus.common.vo.BorrowRecord;
import com.campus.server.dao.*;
import java.util.*;
import java.util.List;

public class LibraryService {
    private BookDao bookDao = new BookDao();
    private BorrowRecordDao borrowRecordDao = new BorrowRecordDao();
    public List<Book> listBooks() {
        return bookDao.listAllBooks();
    }
    


    public List<Book> getUserBorrowedBooks(String username) {
        return bookDao.getUserBorrowedBooks(username);
    }

    public boolean borrowBook(int bookId, String username) {
        return bookDao.borrowBook(bookId, username);
    }

    public boolean returnBook(int bookId, String username) {
        return bookDao.returnBook(bookId, username);
    }
    public boolean addBook(Book book){
        return bookDao.addBook(book);
    }
    
    public boolean deleteBook(int bookId) {
        return bookDao.deleteBook(bookId);
    }

    public List<BorrowRecord> getBookBorrowersById(int bookId) {
        System.out.println("DEBUG: 获取书籍借阅人，书籍ID: " + bookId);

        BorrowRecordDao recordDao = new BorrowRecordDao();
        List<BorrowRecord> records = recordDao.getBorrowRecordsByBookId(bookId);

        System.out.println("DEBUG: 从数据库获取的借阅记录数量: " + (records != null ? records.size() : "null"));

        // 获取用户姓名信息
        UserDao userDao = new UserDao();
        StudentDao studentDao = new StudentDao();
        TeacherDao teacherDao = new TeacherDao();

        for (BorrowRecord record : records) {
            String username = record.getUsername();
            System.out.println("DEBUG: 处理借阅记录，用户名: " + username);

            String role = userDao.getUserRole(username);
            String name = "";

            if ("学生".equals(role)) {
                name = studentDao.getStudentNameById(username);
            } else if ("教师".equals(role)) {
                name = teacherDao.getTeacherNameById(username);
            }

            System.out.println("DEBUG: 用户名: " + username + ", 真实姓名: " + name);
            record.setRealname(name);
        }

        return records;
    }
    public List<BorrowRecord> getUserBorrowRecords(String username) {
        System.out.println("DEBUG: 获取用户借阅记录，用户名: " + username);
        // 需要实现从数据库获取包含日期的借阅记录
        BorrowRecordDao recordDao = new BorrowRecordDao();
        List<BorrowRecord> records = recordDao.getUserBorrowRecords(username);
        System.out.println("DEBUG: 从数据库获取的借阅记录数量: " + (records != null ? records.size() : "null"));
        return records;
    }
    public List<Book> searchBooksByTitle(String title){
        return bookDao.searchBooksByTitle(title);
    }
    
    public List<Book> searchBooksByAuthor(String author){
        return bookDao.searchBooksByAuthor(author);
    }
    
     public List<Book> searchBooks(String title, String author) {
         return bookDao.searchBooks(title, author);
     }
     
     public boolean updateBook(Book book) {
         return bookDao.updateBook(book);
     }
    public List<String> getAllCategories() {
        return bookDao.getAllCategories();
    }

    // 根据类别查询书籍
    public List<Book> searchBooksByCategory(String category) {
        return bookDao.searchBooksByCategory(category);
    }
     public boolean bookExists(int bookId) {
         return bookDao.bookExists(bookId);
     }
}
